package android.test;

import android.app.IntentService;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;

public class MainActivity extends Activity {

    EditText phoneNumber;
    EditText message;
    Button send;
    Button smsReceiver;
    SmsManager smsManager;
    Button call;
    public MediaRecorder Recorder = null;
    public MediaPlayer Player = null;
    public static String fileName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createOb();

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phoneNo = phoneNumber.getText().toString();
                String sms = message.getText().toString();
                try {

                    smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                    Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Send Faild" + e.getStackTrace(), Toast.LENGTH_LONG).show();

                }
            }
        });

        smsReceiver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent in = new Intent(getApplicationContext(), ShowSMS.class);
                startActivity(in);
            }
        });

        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent in = new Intent(Intent.ACTION_CALL);
                in.setData(Uri.parse("tel:"+phoneNumber.getText().toString()));
                startActivity(in);
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        if(Player != null){
            Player.stop();
            Player.release();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void createOb() {
        smsManager = SmsManager.getDefault();
        phoneNumber = (EditText) findViewById(R.id.phone_number);
        message = (EditText) findViewById(R.id.message);
        send = (Button) findViewById(R.id.bt_send);
        smsReceiver = (Button) findViewById(R.id.bt_smsreciever);
        call = (Button)findViewById(R.id.but_call);
    }

    public void startRecord(View v){
//        File mediaFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "RecordApp");
//        if(!mediaFile.exists()){
//            if(!mediaFile.mkdir()){
//                Toast.makeText(getApplicationContext(), "Create Forlder Error!", Toast.LENGTH_LONG).show();
//                return;
//            }
//        }
        fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Audio_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".3gp";
        Recorder = new MediaRecorder();
        Recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        Recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        Recorder.setOutputFile(fileName);
        Recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try{
            Recorder.prepare();
        }catch (IOException e){
            Log.e("Recorder:", "prepare false");
        }

        Recorder.start();
        Toast.makeText(MainActivity.this, "Recording...", Toast.LENGTH_LONG).show();

    }

    public void endRecord(View v){
        Recorder.stop();
        Recorder.release();
        Recorder = null;
        Toast.makeText(MainActivity.this, "Finish Record...", Toast.LENGTH_LONG).show();

    }

    public void play(View v){
        Player = new MediaPlayer();
        try{
            Player.setDataSource(fileName);
            Player.prepare();
            Player.start();
            Toast.makeText(MainActivity.this, "Playing...", Toast.LENGTH_LONG).show();
        }catch (IOException e){
            Log.e("MediaPlayer: ", "prepare false");
        }

    }

    public void callLog(View v){
        Intent i = new Intent(this, CallLogActivity.class);
        startActivity(i);

    }

    public void StartService(View v){
//        Intent i = new Intent(this, IntentServiceTest.class);
//        startService(i);


        try{
            Toast.makeText(MainActivity.this, "sending...", Toast.LENGTH_LONG).show();

            GmailSender mail = new GmailSender("dinhthaobkhn@gmail.com", "thao371992");
            mail.sendMail("Test Mail Android", "hello Android", "dinhthaobkhn@gmail.com", "thaond@sunnet.vn");
            Toast.makeText(MainActivity.this, "send mail successfull!", Toast.LENGTH_LONG).show();

        }catch (Exception e){
            Toast.makeText(MainActivity.this, "error send mail!", Toast.LENGTH_LONG).show();
        }
    }


}
